package cn.linran.basic.controller;

import cn.linran.basic.exception.BusinessException;
import cn.linran.basic.service.IVerifyCodeService;
import cn.linran.basic.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 图形验证码接口
 */
@RequestMapping("/verifyCode")
@RestController
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 图形验证码接口
     * @param key 前端uuid生成的唯一key，
     * @return
     */
    @GetMapping("/image/{key}")
    public JsonResult getVerifyCode(@PathVariable("key") String key){
        try {
            //得到base64位字符串
            String baseStr = verifyCodeService.getVerifyCode(key);
            // 把返回的数据封装到jsonResult中，给页面
            return JsonResult.me().setResultObj(baseStr);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("获取图形验证码失败，稍后重试！");
        }
    }

    /**（点击获取按钮）
     * 短信验证码 1、 接收参数：dto
     *          2、 map接收
     *  map:
     *  phone:  15096190858
     *  imageCode:  abcdef
     *  imageCodeKey: saldfjlsadfjlksd Redis随机生成的key用于存储产生的base64位
     */
    @PostMapping("/smsCode")
    public JsonResult smsCode(@RequestBody Map<String,String> map){
        try {
            //发送短信验证码
            verifyCodeService.smsCode(map);
            return JsonResult.me();
        }catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常，稍后重试");
        }
    }
    /**
     *  微信绑定，获取手机验证码接口
     *  phone:  111111
     *
     */
    @PostMapping("/binderSmsCode")
    public JsonResult binderSmsCode(@RequestBody Map<String,String> map){
        try {
            verifyCodeService.binderSmsCode(map);
            return JsonResult.me();
        }catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常，稍后重试");
        }
    }

}
